//
//  NickNameViewController.swift
//  Exchange
//
//  Created by rui on 2018/5/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class NickNameViewController: AccountBaseViewController {

    lazy var sureBtn: SureButton  = {
        let btn = SureButton()
        btn.setTitle(kText("确定"), for: .normal)
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var nickNameInput: InputView = {
        let input = InputView()
        input.delegate = self
        input.placeholder = kText("请输入昵称")
        return input
    }()
    
    lazy var viewModel: NickNameViewModel = {
        let viewModel = NickNameViewModel()
        viewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_nickName {
                if isSuccess {
                    CYMBShow.showMessage(kText("设置昵称成功"))
                    self?.navigationController?.popViewController(animated: true)
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        return viewModel
    }()
    
    var tipLabel: UILabel = {
        let lbl = UILabel()
        lbl.text = kText("请提交2-16位长度字符昵称")//kText("昵称设置之后不能修改")
        lbl.themeTextColor = ThemeManager.ThemeColorKeys.text2
        lbl.font = UIFont.systemFont(ofSize: 12)
        return lbl
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    func initUI() {
        self.customTitle = kText("昵称")
        let titleHeight = self.titleLabel?.frame.size.height ?? 0
        nickNameInput.frame = CGRect(x: 16, y: titleHeight, width: self.view.frame.size.width - 32, height: 35)
        self.view.addSubview(nickNameInput)
        tipLabel.frame = CGRect(x: 16, y: nickNameInput.bottom + 5, width: nickNameInput.width, height: 20)
        self.view.addSubview(tipLabel)
//        tipLabel.isHidden = true
        sureBtn.frame = CGRect(x: 16, y: tipLabel.bottom + 60, width: nickNameInput.width, height: 40)
        self.view.addSubview(sureBtn)
        sureBtn.isEnabled = false
    }
    
    @objc func sureClick() {
        let nickName = nickNameInput.input.text!
        let isMatch = nickName.count >= 2 && nickName.count <= 16
        if isMatch {
            viewModel.uploadNickName(nickName: nickName)
        }else {
             CYMBShow.showMessage(kText("请提交2-16位长度字符昵称"))
        }
    }

}

extension NickNameViewController : InputViewDelegate {
    func inputViewTextDidChange(inputView: InputView) {
        if var text = inputView.input.text, text.count >= 2 {
            self.sureBtn.isEnabled = true
            if text.count > 16 {
                let startIndex = text.index(text.startIndex, offsetBy: 16)
                let endIndex = text.endIndex
                text.removeSubrange(startIndex..<endIndex)
                inputView.input.text = text
            }
        }else {
            self.sureBtn.isEnabled = false
        }
    }
}
